<!--
Copyright 2009 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->
<!-- Load the Google AJAX API Loader -->
<script type="text/javascript" src="http://www.google.com/jsapi"></script>

<!-- Load the Google Friend Connect javascript library. --> 
<script type="text/javascript">
  google.load('friendconnect', '0.8');
</script>

<!-- Initialize the Google Friend Connect OpenSocial API. -->
<script type="text/javascript">
function runFriendConnectApi() {
  google.friendconnect.container.setParentUrl('/plugins/phpbb/' /* location of rpc_relay.html and canvas.html */);
  google.friendconnect.container.initOpenSocialApi({
       site: '08346674149623660681',
       onload: function(securityToken) { initAllData(securityToken); }
  });
}

function initAllData(securityToken) {
  var req = opensocial.newDataRequest();
  req.add(req.newFetchPersonRequest('VIEWER'), 'viewer');
  req.send(setupData);
}

function setupData(data) {
  var viewer = data.get('viewer').getData();
  if (viewer) {
    document.getElementById('fc_id').value = viewer.getField(opensocial.Person.Field.ID);
    document.getElementById('fc_thumbnail').value =
      viewer.getField(opensocial.Person.Field.THUMBNAIL_URL);
    document.getElementById('fc_displayname').value = viewer.getDisplayName();
    document.getElementById('friend_connect_gadget').innerHTML =
      '' + 
      '<a href="#" onclick="google.friendconnect.requestSettings()">Settings</a> | ' +
      '<a href="#" onclick="google.friendconnect.requestInvite()">Invite</a> | ' +
      '<a href="#" onclick="google.friendconnect.requestSignOut()">Sign out</a>';
    if ('{USERNAME_CREDENTIAL}' != '' && document.getElementById('{USERNAME_CREDENTIAL}')) {
      var login = document.getElementById('{USERNAME_CREDENTIAL}');
      login.value = 'fc_' + viewer.getDisplayName();
    } else if (document.getElementById('username')) {
      var login = document.getElementById('username');
      login.value = 'fc_' + viewer.getDisplayName();
    }
  } else {
    google.friendconnect.renderSignInButton({id: 'friend_connect_gadget', style: 'long', text: 'Join this site'});
  }
}

function mySubmit(username, login_form) {
  // var friendconnectid = document.getElementById('fc_id');
  var friendconnectname = document.getElementById('fc_displayname');
  var re = RegExp("^fc_.+$"); // avoid smarty template language
  if (username.value == '' && friendconnectname.value == '') {
    alert("You are not sign-in on this site.");
    return;
  }
  if (username.value == '' && friendconnectname.value != '') {
    username.value = 'fc_' + friendconnectname.value;
  }
  if (re.test(username.value)) {
    if (username.value != 'fc_' + friendconnectname.value) {
      alert("You are sign-in with others OpensocialId.");
      username.value = '';
      return;
    }
  }
  login_form.submit();
}

</script>

